package day5.demo1_产生流;

import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Demo1 {
    List<Emp> list;
    Stream<Emp> s;

    @Before
    public void initList(){
        list = List.of(new Emp("a1",20),new Emp("a1",30),new Emp("a3",1),new Emp("a4",29));
        s = list.stream();
    }

    @Test
    public void m1(){
        Stream<Emp> s2 = list.parallelStream();
        s.forEach(System.out::println);
        System.out.println("===================");
        s2.forEach(e->{
            System.out.println(Thread.currentThread().getName()+":"+e);
        });
    }

    @Test
    public void m2(){
        String[]arr = "one two three four".split(" ");
        Stream<String> stream3 = Arrays.stream(arr);
        stream3.forEach(System.out::println);
        Stream<Integer> stream4 = Stream.of(1,2,3);
    }
    @Test
    public void m3(){
        Stream<Integer>stream5 = Stream.iterate(0,x->x+2);
        stream5.limit(10).forEach(System.out::println);
    }

    @Test
    public void m4(){
        Stream<Emp> s2 = s.filter(e->e.age>0).skip(0).limit(10).distinct();

//        Stream<Emp> s3 = s2.limit(1);
        s2.forEach(System.out::println);
    }


    @Test
    public void m5(){
        Stream s2=null;
//        Stream<Object> s1 = s.map(new Function<Emp, Integer>() {
//            @Override
//            public Integer apply(Emp emp) {
//                return emp.age;
//            }
//        });
//        Stream<Integer> s2 = s.map(emp -> emp.age);

        IntStream s3 = s.mapToInt((e -> e.age));
        s2.forEach(System.out::println);

    }

    @Test
    public void m6(){
//        System.out.println(s.count());
//        int sum = s.mapToInt(e -> e.age).reduce(0, (num, age) -> num+=age);
//        Optional<Emp> max = s.max((e1, e2) -> e1.age - e2.age);
//        System.out.println(max.get());
//
//        Integer i = null;
//        i.toString();
//        Object[] objects = s.toArray();
//        System.out.println(s.allMatch(e -> e.getAge() > 2));

//        List<Emp> l2 = s.filter(a->a.getAge()>20).collect(Collectors.toList());
//        List<Emp> l2 = list.stream().filter(a->a.getAge()>20).collect(Collectors.toCollection(LinkedList::new));
//        System.out.println(l2.getClass());
        Map<String, Integer> m = s.collect(Collectors.toMap(e -> e.getName(), e -> e.getAge()));
        m.keySet();

    }



}
